<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>交替执行</title>
</head>
<body>
<script>
    var a=1;
    var b=2;
    function *foo(){
        a++;
        console.log("foo:",a,b);
        yield;
        b=b*a;
        console.log("foo:",a,b);
        a=(yield b)+3;
        console.log("foo:",a,b);
    }
    function *bar(){
        b--;
        console.log("bar:",a,b);
        yield;
        console.log("bar:",a,b);
        a=(yield 8)+b;
        console.log("bar:",a,b);
        b=a*(yield 2);
        console.log("bar:",a,b);
    }

    function step(gen){
        var it=gen();
        var last;
        return function(){
            last=it.next(last).value;//把值重新返回，
        }
    }
    var s1=step(foo);
    var s2=step(bar);
//不交叉调用
//    s1();
//    s1();
//    s1();
//
//    s2();
//    s2();
//    s2();
//    s2();
// 交叉调用
    s2();
    s2();
    s1();
    s2();

    s1();

    s1();
    console.log(a,b);
    s2();
    console.log(a,b);

</script>
</body>
</html>